/**
 * 反转链表，并返回链表
 */
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
#define Status int
typedef  int ElemType;

typedef struct LNode{
    ElemType data;
    struct LNode* next;

}LNode,*LinkList;

//初始化
LinkList InitList(){
    LNode *L = (LNode *)malloc(sizeof(LNode));
    if(L == NULL){
        return NULL;
    }
    L->next = NULL;
    return L;
}
//插入
Status ListInsert(LinkList L, int i, ElemType e){
    if(L == NULL){
        return ERROR;
    }
    if(i<1){
        return ERROR;
    }
    LNode *p = L;
    int j = 0;
    while(p!=NULL && j<i-1){
        p = p ->next;
        j++;
    }
    if(p == NULL){
        return ERROR;
    }
    LNode* s = (LNode*)malloc(sizeof(LNode));
    if(s==NULL){
        return ERROR;
    }
    s->data = e;
    s->next = p->next;
    p->next = s;
    return OK;
}
//打印
void PrintList(LinkList L){
    if(L==NULL){
        return;
    }
    LNode *p =L->next;
    while(p!=NULL){
        printf("%d\t",(p->data));
        p = p->next;
    }
    printf("\n");
}
//反转（头节点当哨兵为空）
LinkList LinkReverse(LinkList L){
    if(L==NULL){
        exit(ERROR);
    }
    L=L->next;//去掉头节点
    LNode* prevPtr= NULL;
    LNode* nextPrt=NULL;
    LNode* current=L;
    while(current!=NULL){
        nextPrt=current->next;
        current->next=prevPtr;
        prevPtr=current;
        current=nextPrt;
    }
    //制造头节点
    LNode* head=InitList();
    head->next=prevPtr;
    return head;
}
int main(){
    LinkList L = InitList();
    ListInsert(L,1,1);
    ListInsert(L,2,2);
    ListInsert(L,3,3);
    ListInsert(L,4,4);
    ListInsert(L,5,5);
    ListInsert(L,6,6);
    ListInsert(L,7,3);
    PrintList(L);
    PrintList(LinkReverse(L));
}